﻿Option Strict On
Option Explicit On

Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports FRAMEWORK
Imports System.Drawing.Drawing2D

Public Class frm_PaperType
    Inherits asWins.Forms.BaseForm

    'Global Variable
    Dim DA As New FRAMEWORK.SQLDataAccess

#Region " Function and Procedure "
    Private Sub Handler()
        ControlGotFocus(Me, Me)
        ControlLostFocus(Me, Me)
    End Sub
    Private Sub ControlGotFocus(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
        Dim ctr As Control
        For Each ctr In parentCtr.Controls
            If Not (ctr Is ignoreControl) Then
                If TypeOf ctr Is TextBox Then
                    AddHandler ctr.GotFocus, AddressOf EvenHand.KeyEventHand.TextBox_GotFocus
                ElseIf TypeOf ctr Is ComboBox Then
                    AddHandler DirectCast(ctr, ComboBox).GotFocus, AddressOf EvenHand.KeyEventHand.ComboBox_GotFocus
                End If
            End If
            ControlGotFocus(ctr, ignoreControl)
        Next
    End Sub
    Private Sub ControlLostFocus(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
        Dim ctr As Control
        For Each ctr In parentCtr.Controls
            If Not (ctr Is ignoreControl) Then
                If TypeOf ctr Is TextBox Then
                    AddHandler ctr.LostFocus, AddressOf EvenHand.KeyEventHand.TextBox_LostFocus
                ElseIf TypeOf ctr Is ComboBox Then
                    AddHandler DirectCast(ctr, ComboBox).LostFocus, AddressOf EvenHand.KeyEventHand.ComboBox_LostFocus
                End If
            End If
            ControlLostFocus(ctr, ignoreControl)
        Next
    End Sub

    Private Function Get_Code() As String
        Dim NewCode As String = ""
        Dim para(5) As SqlParameter

        para(0) = New SqlParameter("@ACTION", 0)
        para(1) = New SqlParameter("@PT_Code", "")
        para(2) = New SqlParameter("@PT_Name", "")
        para(3) = New SqlParameter("@PT_Remark", "")
        para(4) = New SqlParameter("@PT_Status", 0)
        NewCode = DA.ExecuteScalar(CommandType.StoredProcedure, "sp_SYS_PAPERTYPE", para).ToString
        Return NewCode
    End Function
    Private Sub Get_ALL_PaperType()
        Dim dt As DataTable

        Dim para(5) As SqlParameter

        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@PT_Code", "")
        para(2) = New SqlParameter("@PT_Name", "")
        para(3) = New SqlParameter("@PT_Remark", "")
        para(4) = New SqlParameter("@PT_Status", 0)
        dt = DA.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_PAPERTYPE", para).Tables(0)

        If dt.Rows.Count > 0 Then
            dgvDetails.DataSource = dt
            FormatHeader()
        Else
            dgvDetails.DataSource = Nothing
        End If


    End Sub
    Private Sub FormatHeader()
        With dgvDetails
            If .RowCount > 0 Then
                .Columns(0).HeaderText = "รหัส"
                .Columns(1).HeaderText = "ชื่อ"
                .Columns(2).HeaderText = "คำอธิบาย"
                .Columns(3).HeaderText = "สถานะ"

                .Columns(0).Width = 100
                .Columns(1).Width = 250
                .Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns(3).Visible = False
            End If
        End With
    End Sub
    Private Sub ClearData(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
        Dim ctr As Control
        For Each ctr In parentCtr.Controls
            If Not (ctr Is ignoreControl) Then
                If TypeOf ctr Is TextBox Then
                    ctr.Text = ""
                ElseIf TypeOf ctr Is ComboBox Then
                    DirectCast(ctr, ComboBox).SelectedIndex = -1
                ElseIf TypeOf ctr Is ListBox Then
                    CType(ctr, ListBox).Items.Clear()
                End If
            End If
            ClearData(ctr, ignoreControl)
        Next
        btnAdd.Enabled = True
    End Sub

    Private Sub Execute_Data()
        If txtName.Text.Trim() = "" Then
            MessageBox.Show("กรุณาป้อนชื่อก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtName.Focus()
            Exit Sub
        End If

        Dim L_complete As Integer = 0
        If MessageBox.Show("คุณต้องการเพิ่มข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Try
                DA.BeginTransaction()
                Dim para(5) As SqlParameter

                para(0) = New SqlParameter("@ACTION", 1)
                para(1) = New SqlParameter("@PT_Code", "")
                para(2) = New SqlParameter("@PT_Name", txtName.Text)
                para(3) = New SqlParameter("@PT_Remark", txtRemark.Text)
                para(4) = New SqlParameter("@PT_Status", 0)
                L_complete = DA.ExecuteNonQuery(DA.Transaction, CommandType.StoredProcedure, "sp_SYS_PAPERTYPE", para)
                If L_complete > 0 Then
                    DA.CommitTransaction()
                    MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Call ClearData(Me, Me)
                    Call Get_ALL_PaperType()
                Else
                    DA.RollBackTransaction()
                    MessageBox.Show("เกิดข้อผิดพลาด", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                End If

            Catch ex As Exception
                DA.RollBackTransaction()
                MessageBox.Show("เกิดข้อผิดพลาด เนื่องจาก " & ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End Try

            txtName.Focus()

        Else
            Exit Sub
        End If
    End Sub
    Private Sub Update_Data(ByVal index As Integer, ByVal tmp As String)
        If txtCode.Text.Trim() = "" Then
            MessageBox.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtCode.Focus()
            Exit Sub
        End If
        If txtName.Text.Trim() = "" Then
            MessageBox.Show("กรุณาป้อนชื่อก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtName.Focus()
            Exit Sub
        End If

        Dim L_complete As Integer = 0
        If MessageBox.Show("คุณต้องการ" & tmp & "ข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Try
                DA.BeginTransaction()
                Dim para(5) As SqlParameter

                para(0) = New SqlParameter("@ACTION", 2)
                para(1) = New SqlParameter("@PT_Code", txtCode.Text)
                para(2) = New SqlParameter("@PT_Name", txtName.Text)
                para(3) = New SqlParameter("@PT_Remark", txtRemark.Text)
                para(4) = New SqlParameter("@PT_Status", index)
                L_complete = DA.ExecuteNonQuery(DA.Transaction, CommandType.StoredProcedure, "sp_SYS_PAPERTYPE", para)
                If L_complete > 0 Then
                    DA.CommitTransaction()
                    MessageBox.Show(tmp & "ข้อมูลเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Call ClearData(Me, Me)
                    Call Get_ALL_PaperType()
                Else
                    DA.RollBackTransaction()
                    MessageBox.Show("เกิดข้อผิดพลาด", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                End If

            Catch ex As Exception
                DA.RollBackTransaction()
                MessageBox.Show("เกิดข้อผิดพลาด เนื่องจาก " & ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End Try

            txtName.Focus()

        Else
            Exit Sub
        End If
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim lgb As New LinearGradientBrush(ClientRectangle, Color.Honeydew, Color.Aquamarine, LinearGradientMode.Vertical)
        e.Graphics.FillRectangle(lgb, ClientRectangle)

        lgb.Dispose()
    End Sub
#End Region

    Private Sub dgvDetails_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvDetails.CellMouseUp
        If e.RowIndex = -1 Then Exit Sub
        btnAdd.Enabled = False
        With dgvDetails
            txtCode.Text = .Rows.Item(e.RowIndex).Cells(0).Value.ToString()
            txtName.Text = .Rows.Item(e.RowIndex).Cells(1).Value.ToString()
            txtRemark.Text = .Rows.Item(e.RowIndex).Cells(2).Value.ToString()
            txtName.Focus()
            txtName.SelectAll()
        End With
    End Sub

    Private Sub dgvDetails_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgvDetails.RowPostPaint
        Using b As SolidBrush = New SolidBrush(Me.dgvDetails.RowHeadersDefaultCellStyle.ForeColor)

            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), _
                                    Me.dgvDetails.DefaultCellStyle.Font, _
                                    b, e.RowBounds.Location.X + 5, _
                                    e.RowBounds.Location.Y + 5)
        End Using
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Execute_Data()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Update_Data(0, "แก้ไข") 'Edit
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ClearData(Me, Me)
        btnAdd.Enabled = True
        txtName.Focus()
        Get_ALL_PaperType()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Update_Data(1, "ลบ") 'Cancel
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frm_PaperType_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim fd As New FormFader
        fd.FadeOut(Me, 0.25)
    End Sub

    Private Sub frm_PaperType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Handler()
        GroupBox1.BackColor = Color.FromArgb(0, 0, 0, 0)
        GroupBox2.BackColor = Color.FromArgb(0, 0, 0, 0)
        GroupBox3.BackColor = Color.FromArgb(0, 0, 0, 0)
        Dim FD As New FormFader
        FD.FadeIn(Me, 0.25)
        Get_ALL_PaperType()
    End Sub
End Class